////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#ifndef __WN_SCRIPTING_SCRIPTING_ENGINE_H__
#define __WN_SCRIPTING_SCRIPTING_ENGINE_H__

#include "WNCore/inc/WNTypes.h"
#include "WNScripting/inc/WNScriptingEnginePublic.h"
#include "WNScripting/inc/WNScriptingConfig.h"
#include "WNScripting/inc/WNIdentifierMap.h"
#include "WNScripting/inc/WNScriptingEngineInternal.h"
#include "WNScripting/inc/WNScopedVariableList.h"
#include "WNScripting/inc/WNScriptingEngineData.h"
#include "WNScripting/inc/WNTypeElement.h"
#include "WNConcurrency/inc/WNRecursiveMutex.h"

namespace WNScripting {
    class WNScriptingEngineImpl : public WNScriptingEngine {
    public:
        virtual ~WNScriptingEngineImpl();
        virtual WN_BOOL AddExternalFunction(WN_VOID* _funcPtr, WN_BOOL _stackRet, const WN_CHAR* _returnType, const WN_CHAR* _name, const WN_CHAR* _arglist);
        virtual WN_BOOL RunFunction(const WN_CHAR* _file, const WN_CHAR* _functionName, const std::vector<WNParameter>& _params, WNParameter& _outParam);
        virtual WN_VOID CompileFunction(WNFunction* _func);

        virtual WN_BOOL Initialize();
        virtual WN_VOID DumpModule();
        virtual WNParameter CreateParameter(WN_CHAR* _type);
        virtual WN_VOID SetMallocFunction(WNScriptAllocFunc _scriptFunc);
        virtual WN_VOID SetFreeFunction(WNScriptFreeFunc _freeFunc);
        virtual WN_VOID SetMemcpyFunction(WNScriptMemcpy _memcpyFunc);
        virtual WN_VOID AddThread();

        void SetIsHeap(WN_BOOL _isHeap) { mIsHeap = _isHeap; }
        WN_BOOL GetIsHeap() { return(mIsHeap); }

    private:
        WN_BOOL RunAssociatedFunction(const WNIdentifierElement& _functionName, const WNIdentifierElement& _file);
        WN_BOOL QuickParseFile(WNIdentifierElement _name);

        WN_UNORDERED_SET<WNIdentifierElement>   mCheckedFiles;
        WNScriptingEngineData                   mScriptingEngineData;
        WN_CHAR                                 mErrorString[512];
        WN_BOOL                                 mIsHeap;

        #ifdef WN_SCRIPTING_MULTITHREADED_PARSING
            WNConcurrency::WNRecursiveMutex mScriptingEngineLock;
        #endif
    };
}
#endif // __WN_SCRIPTING_SCRIPTING_ENGINE_H__
